//
//  SSSMerchantHistoryBenefitViewController.m
//  SSSAllRollAgency
//
//  Created by xnxin on 2017/3/15.
//  Copyright (c) 2017 Darker. All rights reserved.
//

#import "SSSMerchantHistoryBenefitViewController.h"
#import "SSSMerchantHistoryBenefitCellModel.h"
#import "XHKitMacro.h"
#import "UIViewAdditions.h"
#import "UIView+XHAdd.h"
#import "XHUIHelper.h"
#import "APITool.h"
#import "NSDictionaryAdditions.h"
#import "SSSMerchantBenefitSearchDetailController.h"
#import "XHTableViewSection.h"
#import "XHBaseTableViewCell.h"

@interface SSSMerchantHistoryBenefitViewController ()

@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UILabel *totalBenefitLabel;
@property (nonatomic, strong) NSString *totalBenefit;

@end

@implementation SSSMerchantHistoryBenefitViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView.rowHeight = 50;
    self.tableView.left = 15;
    self.tableView.width = DEVICE_WIDTH - 30;
    self.tableView.backgroundColor = self.view.backgroundColor;
    self.tableView.tableHeaderView = self.headerView;
    self.totalBenefit = @"0";

}

- (NSArray<NSString *> *)classNameArray {
    return @[@"SSSMerchantHistoryBenefitCell"];
}

- (NSArray*)processData:(NSDictionary *)data {

    self.totalBenefit = [[data objectOrNilForKey:@"totalMoney"] description];
    NSArray *modelArray = [NSArray modelArrayWithClass:[SSSMerchantHistoryBenefitCellModel class]
                                                  json:[[data objectOrNilForKey:@"page"] objectOrNilForKey:@"pageList"]];
    return modelArray;
}

- (NSString *)url {
    return [APITool urlStr:@"/bApi/myIncome/historyIncome"];
}

#pragma mark - getter

- (UIView*)headerView {

    if (!_headerView) {

       _headerView = [UIView new];
        _headerView.height = 36;
        _headerView.width = DEVICE_WIDTH - 30;
        _headerView.backgroundColor = [UIColor whiteColor];

        [_headerView addSubview:self.todayBenefitLabel];

        UIView *line = [XHUIHelper sepreateLineWithY:35.5];
        line.left = 12;
        line.width = _headerView.width - 24;
        [_headerView addSubview:line];
    }
    return _headerView;
}

- (UILabel *)todayBenefitLabel {

    if (!_totalBenefitLabel) {

        _totalBenefitLabel = [UILabel new];
        _totalBenefitLabel.font = [UIFont systemFontOfSize:14];
        _totalBenefitLabel.textColor = [UIColor colorWithHex:@"57C18B"];
    }
    return _totalBenefitLabel;
}

#pragma mark -setter

- (void)setTotalBenefit:(NSString *)totalBenefit {
    _totalBenefit = totalBenefit;

    self.todayBenefitLabel.text = [NSString stringWithFormat:@"总额： %@", totalBenefit];
    [self.totalBenefitLabel sizeToFit];
    self.totalBenefitLabel.centerX = self.headerView.width/2;
    self.totalBenefitLabel.centerY = self.headerView.height/2;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    SSSMerchantHistoryBenefitCellModel *model = [self.dataSource modelForIndexPath:indexPath];
    SSSMerchantBenefitSearchDetailController *vc = [SSSMerchantBenefitSearchDetailController new];
    vc.chooseDate = model.date;
    [self.navigationController pushViewController:vc animated:true];
    [tableView deselectRowAtIndexPath:indexPath animated:true];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(XHBaseTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    if ([self.dataSource isLastCellForIndexPath:indexPath]) {
        cell.hidenGrayLine = YES;
    }
}
@end
